
import delimited "${data2}mw_2020.csv",clear
tempfile 2020
save `2020'

use  "${data2}mw_state_annual.dta",clear
rename statefips state_fips
rename  mean_mw mw

* rename mw mw_1
* use "${data2}nominalmw_annual_withlags.dta",clear

merge m:1 state_fips using "${data2}state_geocodes.dta", nogen keep(3)
keep state_fips year mw state_name state_abb division region

tempfile mw
save `mw'

keep if year ==2019
replace year = 2020
merge 1:1 state_name using `2020', nogen keep(3)
append using `mw'

* want mw to be the same year as reported income
* income reported in t is from t-1
* mw from t-1 should be match to t

merge m:1 year using "${data2}cpi_fred.dta", nogen keep(1 3)
merge 1:1 state_fips year using "${data2}aca_expansion_eitc.dta" , nogen keep(1 3)

* rename mw mw_1
* merge 1:1 state_fips year using "${data2}nominalmw_annual_withlags.dta"

xtset state_fips year
* convert to $2019
* replace mw = mw*inflator


gen F3mw = F3.mw
gen F2mw = F2.mw
gen F1mw = F1.mw
gen L1mw = L1.mw
gen L2mw = L2.mw
gen L3mw = L3.mw
gen L4mw = L4.mw
order state_fips year *F* mw *L*



gen mw_d1=d1.mw
* gen mw_D1=d1.mw if year >2009
cap drop D1
* gen change = round(mw_d1,.01)
* tabout  change  using "${est}mw_changes.csv" if year > 2010 , replace style(csv) cells(freq col)



* gen D1 = 0
* replace D1 = 1 if year >=2013 & inlist(state_abb,"RI","SD")
* replace D1 = 1 if year >=2014 & inlist(state_abb,"CA","CT","DC","MA","MN","NJ","NY")
* replace D1 = 1 if year >=2015 & inlist(state_abb,"AK","HI","MD","NE","VT")
* replace D1 = 1 if year >=2016 & inlist(state_abb,"OR")
* replace D1 = 1 if year >=2017 & inlist(state_abb,"AZ","CO","ME","WA")
* gen D2= 0
* replace D2 = 1 if year >=2011 & inlist(state_abb,"FL","IL","MT","NV","OH")
* replace D2 = 1 if year >=2013 & inlist(state_abb,"MO")
* replace D2 = 1 if year >=2014 & inlist(state_abb,"DE")
* replace D2 = 1 if year >=2015 & inlist(state_abb,"AR")
* local Ds D1 D2
* * local D D3
* foreach D of local Ds  {
*   gen `D'_L0=d1.`D'
*   replace `D'_L0= 0 if `D'_L0==.
*   label var `D'_L0 "0"
*   local first 3
*   local last 2
*   forval f = `first'(-1)1 {
*   sort state_fips  year
*   gen `D'_F`f' = F`f'.`D'_L0
*   label var  `D'_F`f' "-`f'"
*   replace `D'_F`f' = 0 if `D'_F`f'==.
*   if `f' ==`first' label var  `D'_F`f' "-`f'+"
*   }
*   forval l = 1/`last'  {
*   gen `D'_L`l' = L`l'.`D'_L0
*   label var  `D'_L`l' "`l'"
*   replace `D'_L`l' = 0 if `D'_L`l'==.
*   if `l' ==`last' label var  `D'_L`l' "`l'+"
*   }
*   order `D'_F* `D'_L* , last
*   gsort state_fips -year
*   by state_fips : replace `D'_F`first' = sum(`D'_F`first')
*   gsort state_fips  year
*   by state_fips : replace `D'_L`last' = sum(`D'_L`last')
* }

* gen cmw =mw_d1/mw
* by state_fips: gen cmw = sum(mw_d1)
* gen dmw = floor(mw_d1)

local v mw_d1


g lnMWF3 = ln(F3mw)
g lnMWF2 = ln(F2mw)
g lnMWF1 = ln(F1mw)
g lnMW   = ln(mw)
g lnMWL1 = ln(L1mw)
g lnMWL2 = ln(L2mw)
g lnMWL3 = ln(L3mw)

replace year = year+1
keep if year >${sample_start}

local first 3
local last 3
forval f = `first'(-1)1 {
sort state_fips  year
gen EF`f' = F`f'.`v'
label var  EF`f' "-`f'"
replace EF`f' = 0 if EF`f'==.
if `f' ==`first' label var  EF`f' "-`f'+"
}

forval l = 0/`last'  {
gen EL`l' = L`l'.`v'
label var  EL`l' "`l'"
replace EL`l' = 0 if EL`l'==.
if `l' ==`last' label var  EL`l' "`l'+"
}


order EF* EL* , last
gsort state_fips  -year
by state_fips : replace EF`first' = sum(EF`first')
gsort state_fips  year
by state_fips : replace EL`last' = sum(EL`last')



* gen BEL_0_1 = EL0 +EL1
gen BEL_0_2 = EL0 +EL1 +EL2


* * replace mw_d1=round(mw_d1,.01)
* * keep if year > 2010
* * merge m:1 state_fips using "${data2}state_geocodes.dta", nogen keep(3)
* *
* * rename mw_d1 _
* * * format _ %3.2f
* * tostring _, replace usedisplayformat force
* * destring _, replace
* * keep year state_name _
* * reshape wide _, i(state_name) j(year)
* * export delimited "${initialpath}state_mw.csv",replace
